package cn.hg.jfsa.web.theme;

import java.io.IOException;

import com.jfinal.kit.StrKit;
import com.jfinal.template.Env;
import com.jfinal.template.TemplateException;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import cn.hg.base.jfinal.directive.annotation.JfsaDirective;
import cn.hg.jfsa.common.directive.BaseDirective;

@JfsaDirective ("themeOption")
public class ThemeOptionDirective extends BaseDirective {

	@Override
	public void onRender (Env env, Scope scope, Writer writer) {
		String key = this.getPara(0, scope);

		if (StrKit.isBlank(key)) {
			throw new IllegalArgumentException("#themeOption(...) argument must not be empty");
		}

		String defaultValue = this.getPara(1, scope, "");
		String value = ThemeManager.me().getCurrentTheme().getOption(key);

		if (StrKit.isBlank(value)) {
			value = defaultValue;
		}

		try {
			writer.write(value);
		}
		catch (IOException e) {
			throw new TemplateException(e.getMessage(), this.location, e);
		}

	}

}
